package fr.lip6.move.care;

import fr.lip6.move.care.CareGen;
import fr.lip6.move.care.CompileResult;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import sdmakerz_v1.coresyntax.SDMakerV1Utils;
import sdmakerz_v1.coresyntax.generator.SDMakerV1IDProvider;
import sdmakerz_v1.coresyntax.generator.interfaces.IConfigData;
import sdmakerz_v1.coresyntax.generator.interfaces.IContext;
import sdmakerz_v1.coresyntax.generator.interfaces.IDiagramRefData;
import sdmakerz_v1.coresyntax.generator.interfaces.IInputData;
import sdmakerz_v1.coresyntax.generator.interfaces.IInterfaceData;
import sdmakerz_v1.coresyntax.sDMakerV1.Diagram;
import sdmakerz_v1.coresyntax.sDMakerV1.DiagramElement;
import sdmakerz_v1.coresyntax.sDMakerV1.Obj;
import sdmakerz_v1.coresyntax.sDMakerV1.Type;

@SuppressWarnings("all")
public class CareDiagramGen {
  private CareGen mainGenerator;
  
  public CareDiagramGen(final CareGen mainGenerator) {
    this.mainGenerator = mainGenerator;
  }
  
  public CompileResult compile(final IContext context, final Diagram diagram) {
    ArrayList<IInterfaceData> _arrayList = new ArrayList<IInterfaceData>();
    List<IInterfaceData> usedClass = _arrayList;
    ArrayList<CompileResult> _arrayList_1 = new ArrayList<CompileResult>();
    List<CompileResult> compileResults = _arrayList_1;
    EList<DiagramElement> _diagram_elts = diagram.getDiagram_elts();
    for (final DiagramElement elt : _diagram_elts) {
      {
        if ((elt instanceof Obj)) {
          Obj obj = ((Obj) elt);
          Type _type = obj.getType();
          String _identifier = _type.getIdentifier();
          IInterfaceData iid = context.getInterfaceDataByName(_identifier);
          boolean _contains = SDMakerV1Utils.contains(usedClass, iid);
          boolean _equals = (_contains == false);
          if (_equals) {
            usedClass.add(iid);
          }
        }
        CompileResult compRes = this.mainGenerator.compile(context, elt);
        Boolean _isAddableToEntryPoint = compRes.getIsAddableToEntryPoint();
        if ((_isAddableToEntryPoint).booleanValue()) {
          this.mainGenerator.addToMainSequence(compRes);
        }
        compileResults.add(compRes);
      }
    }
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package programs;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.ArrayList;");
    _builder.newLine();
    _builder.append("import program.*;");
    _builder.newLine();
    _builder.append("import traces.*;");
    _builder.newLine();
    IConfigData _configData = context.getConfigData();
    CharSequence _printImports = _configData.printImports();
    _builder.append(_printImports, "");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("public class ");
    IInputData _inputData = context.getInputData();
    String _outputBasedName = _inputData.getOutputBasedName();
    String _replaceAll = _outputBasedName.replaceAll("/", "\\.");
    String _computeSimpleName = SDMakerV1Utils.computeSimpleName(_replaceAll);
    _builder.append(_computeSimpleName, "");
    _builder.append("_");
    String _name = diagram.getName();
    _builder.append(_name, "");
    _builder.append(" {     ");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("public static Main create(){");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("int i= 0;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("ArrayList<Actor> actors= new ArrayList<Actor>();");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("ObjectClass actorClass=new ObjectClass(\"Actor\");");
    _builder.newLine();
    {
      for(final IInterfaceData interfaceDatas : usedClass) {
        _builder.append("        ");
        _builder.append("ObjectClass ");
        String _interfaceID = interfaceDatas.getInterfaceID();
        _builder.append(_interfaceID, "        ");
        _builder.append("= new ObjectClass(\"");
        String _qualifiedName = interfaceDatas.getQualifiedName();
        _builder.append(_qualifiedName, "        ");
        _builder.append("\");");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("        ");
    _builder.newLine();
    {
      for(final CompileResult compRes : compileResults) {
        _builder.append("        ");
        CharSequence _charSequenceResult = compRes.getCharSequenceResult();
        _builder.append(_charSequenceResult, "        ");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    int _computeID = SDMakerV1IDProvider.computeID();
    String mainID = ("entry_point_id" + Integer.valueOf(_computeID));
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("ArrayList<Block> ");
    _builder.append(mainID, "        ");
    _builder.append(" = new ArrayList<Block>();");
    _builder.newLineIfNotEmpty();
    {
      List<CompileResult> _mainSequence = this.mainGenerator.getMainSequence();
      for(final CompileResult elt_1 : _mainSequence) {
        _builder.append("        ");
        _builder.append(mainID, "        ");
        _builder.append(".add(");
        String _eltID = elt_1.getEltID();
        _builder.append(_eltID, "        ");
        _builder.append(");");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("        ");
    _builder.append("return new Main(");
    _builder.append(mainID, "        ");
    _builder.append(", actors, 200);");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("} ");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public static void main(String[] args) {");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Main sd=create();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("boolean ok=sd.saveModel(\"");
    IDiagramRefData _diagramRefData = context.getDiagramRefData();
    String _diagNewName = _diagramRefData.diagNewName();
    _builder.append(_diagNewName, "        ");
    _builder.append("\");");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("if (ok){");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("sd.genereAllTraces(\"");
    IDiagramRefData _diagramRefData_1 = context.getDiagramRefData();
    String _diagNewName_1 = _diagramRefData_1.diagNewName();
    _builder.append(_diagNewName_1, "            ");
    _builder.append("\", 100);");
    _builder.newLineIfNotEmpty();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("        ");
    _builder.newLine();
    _builder.append("    \t        ");
    IConfigData _configData_1 = context.getConfigData();
    CharSequence _printEval = _configData_1.printEval();
    _builder.append(_printEval, "    	        ");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    CharSequence charSequenceResult = _builder;
    CompileResult _compileResult = new CompileResult(charSequenceResult, 
      null, 
      true);
    return _compileResult;
  }
}
